package com.yun.algorithmproblem.leetcode;

import java.util.LinkedList;

public class Leetcode946 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        LinkedList<Integer> stack = new LinkedList<>();
        int p = 0, q = 0;
        while (p < popped.length) {
            if (stack.peek() != null && stack.peek() == popped[p]) {
                stack.pop();
                p++;
            } else if (q < pushed.length) {
                stack.push(pushed[q]);
                q++;
            }else {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int[] pushed={1,2,3,4,5};
        int[] popped={4,3,5,1,2};
        Leetcode946 obj=new Leetcode946();
        System.out.println(obj.validateStackSequences(pushed,popped));
    }
}
